#include "type.h"

void main(int argc, char* argv[])
{
	int consoleChild = 0, serialChild1 = 0, serialChild2 = 0;
	int stdin = open("/dev/tty0", READ);
	int stdout = open("/dev/tty0", WRITE);
	
	printf("JRINIT: forking the login children\n");
	
	consoleChild = do_fork();	
	if (consoleChild > 0)
	{
		serialChild1 = do_fork();
		if (serialChild1 > 0)
		{
			serialChild2 = do_fork();
			if (serialChild2 > 0)
			{
				while(1)
				{
					int pid = 0, status = 0;
					printf("JRINIT: waiting...\n");
					pid = wait(&status);
					
					printf("Pid %d exited with status %d\n", pid, status);
					
					if (pid == consoleChild)
					{
						consoleChild = do_fork();
						if (consoleChild == 0)
						{
							do_exec("login /dev/tty0");
						}
					}
					else if (pid == serialChild1)
					{
						serialChild1 = do_fork();
						if (serialChild1 == 0)
						{
							do_exec("login /dev/ttyS0");
						}
					}
					else if (pid == serialChild2)
					{
						serialChild2 = do_fork();
						if (serialChild2 == 0)
						{
							do_exec("login /dev/ttyS1");
						}
					}
					else
					{
						printf("JRINIT: Buried process %d with status %d\n", pid, status);
					}
				}
			}
			else
			{
				do_exec("login /dev/ttyS1");
			}
		}
		else
		{
			do_exec("login /dev/ttyS0");
		}
	}
	else
	{
		do_exec("login /dev/tty0");
	}
}
